package com.woniu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.Customer;
import com.woniu.service.CustomerService;
import com.woniu.mapper.CustomerMapper;
import com.woniu.vo.CustomerVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Apricity
* @description 针对表【customer(客户表)】的数据库操作Service实现
* @createDate 2025-02-13 20:16:58
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    @Override
    public List<CustomerVO> getCustomerList(String keyword, Integer pageNum, Integer pageSize) {
        Page<CustomerVO> page = new Page<>(pageNum, pageSize);
        List<CustomerVO> customers;
        
        if (keyword != null && !keyword.trim().isEmpty()) {
            customers = customerMapper.queryCustomerByKeyword(keyword, page);
        } else {
            customers = customerMapper.queryCustomerList(page);
        }
        
        return customers;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCustomer(Customer customer) {
        return customerMapper.insert(customer) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCustomer(Long id) {
        return customerMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCustomer(Customer customer) {
        if (customer.getId() == null) {
            return false;
        }
        return customerMapper.updateCustomer(customer) > 0;
    }
}




